<?php
require_once 'BaseAction.php';

class ModeController extends TT_Base_Action
{
    public function indexAction()
    {
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->level != 1){  // Chuyen ve trang login neu khong phai la Mode
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
    }
    
    // Cat chuoi lay thoi gian
    private function time1($str)
    {
        if($str == ''){
            $time2 = 0;
        }else{
            $time1 = explode("-",$str);
            $time2 = mktime(0,0,0,$time1[1],$time1[0],$time1[2]);
        }
        return $time2;
    }
    
    // Kiem tra ngay
    private  function checkdate1($date)
        {
             $aDate_parts =explode("-",$date);


         if((int)$aDate_parts[0]!=0 && (int)$aDate_parts[2] !=0 && (int)$aDate_parts[2]!=0 )  
            {
                  if(checkdate($aDate_parts[1],$aDate_parts[0],$aDate_parts[2]))
                {
                    $a=1;
                }
            else
                {
                    $a=2 ;
                }  
            }
         else
            {
                $a=0;
            }
         return $a;
        }
    
    // Ham tao user moi
    public function addnewAction()
    {
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->level !=1){  // Chuyen ve trang login neu khong phai la Mode
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel("Modes");
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        if($this->getRequest()->isPost())
        {
            $username = trim($this->_getParam('username'));
            $password = $this->_getParam('password');
            $password = md5($password);
            $password2 = $this->_getParam('password2');
            $password2 = md5($password2);
            /*
            $admin = $this->_getParam('admin');
            $level = $this->_getParam('level');
            $unauthorized = $this->_getParam('unauthorized');
            */
            
            //  && $admin !="" && $level !="" && $unauthorized !=""
            
            if($username !="" && $password !="e10adc3949ba59abbe56e057f20f883e")
            {
                if($password == $password2)
                {
                    $check = $u_model->checkName($username); // Kiem tra ten moi co nguoi dung chua?
                    if($check == true) // Neu chua co ng dung
                    {
                        if(strpos($username," "))
                        {
                            $this->view->thongbao4 = 1;
                        }else{
                            if(strlen($username) <= 100)
                            {
                                $this->view->thongbao0 = 1; // Thong bao da xong
                                $a = $u_model->new_user($username,$password);
                            }else{
                                $this->view->thongbao5 = 1;
                            }
                        }
                    }
                    if($check == false){
                        $this->view->thongbao2 = 1; // Thong bao neu user da co nguoi dung
                    }
                }
                else{
                    $this->view->thongbao3 = 1; // Thong bao neu pass xac nhan khong giong
                }
            }
            else{
                $this->view->thongbao1 = 1; // Thong bao neu chua dien thong tin day du
            }
        }
    }
    
    // Ham xoa user
    public function removeuserAction()
    {
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->level !=1){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel('Modes');
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        if($this->getRequest()->isPost())
        {
            $username = trim($this->_getParam('username'));
            $a = $u_model->removeuser($username);
            
            $this->view->name = $username;
            $this->view->thongbao = 1;
        }
        
        //$mem = $u_model->members();
        //$this->view->mem = $mem;
        $mem = $u_model->members($page_start,$page_rows);
        $this->view->mem = $mem;
        $num=$u_model->members_num();
        $this->view->num = $num;
    }
    
    // Cam user dang nhap
    public function addunauthorizedAction()
    {   
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->level !=1){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel('Modes');
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        if($this->getRequest()->isPost())
        {
            $username = trim($this->_getParam('username'));
            $a = $u_model->addunauthorized($username);
            
            $this->view->name = $username;
            $this->view->thongbao = 1;
        }
        //$mem = $u_model->members();
        //$this->view->mem = $mem;
        $mem = $u_model->members($page_start,$page_rows);
        $this->view->mem = $mem;
        $num=$u_model->members_num();
        $this->view->num = $num;
        
        $unauthorized = $u_model->unauthorized_user();
        $this->view->unauthorized = $unauthorized;
    }
    
    // Bo cam user dang nhap
    public function removeunauthorizedAction()
    {
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->level !=1){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel('Modes');
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        if($this->getRequest()->isPost())
        {
            $username = trim($this->_getParam('username'));
            $a = $u_model->removeunauthorized($username);
            
            $this->view->name = $username;
            $this->view->thongbao = 1;
        }
        //$mem = $u_model->members();
        //$this->view->mem = $mem;
        $mem = $u_model->members($page_start,$page_rows);
        $this->view->mem = $mem;
        $num=$u_model->members_num();
        $this->view->num = $num;
        
        $unauthorized = $u_model->unauthorized_user();
        $this->view->unauthorized = $unauthorized;
    }
    
    // Tao moi giao dich vay
    public function addnewloansAction()
    {
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->level != 1)
        {
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel("Modes");
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        if($this->getRequest()->isPost())
        {
            $user_loan = trim($this->_getParam('user_loan'));
            $user_squama = trim($this->_getParam('user_squama'));
            
            $check=$u_model->checkstatus($user_squama);
            //echo $check->status;
            
            $date_started = $this->_getParam('date_started');
            //$date_started = $this->time1($date_started);
            $amount = $this->_getParam('amount');
            $duration = $this->_getParam('duration');
            //$duration = $this->time1($duration);
            //$status = $this->_getParam('status');     
            $description = $this->_getParam('description');
            //$updated = $this->_getParam('updated');
                    
            if($date_started=="")
                {
                    $date_started=0;
                }
            else
                {
                    $checkdate_started=$this->checkdate1($date_started);
                }
                
            if($duration=="")
                {
                    $duration=0;
                }
            else
                {
                    $check_duration=$this->checkdate1($duration);
                }

            if($user_loan !="" && $date_started !=0 && $user_squama !="" && $amount !="" && $duration !=0)
            {   
                if($checkdate_started == 1 && $check_duration == 1)
                {
                    $date_started = $this->time1($date_started);
                    $duration = $this->time1($duration);
                    if($date_started > 943894800  && $duration > 943894800)                          
                    {
                        if($duration > $date_started)
                        {
                            if($amount > 0)
                            {
                                if($check->status == 1){
                                    $a = $u_model->new_loan($user_squama,$user_loan,$date_started,$duration,$amount,$description);
                                    $this->view->thongbao0 = 1; // Thong bao thanh cong         
                                }else{
                                    
                                   $this->view->thongbao6 =1; 
                                }
                                
                            }else
                            {
                                $this->view->thongbao5 =1; // so luong phai kieu so va lon hon 0
                            }                            
                        }
                        else
                        {
                            $this->view->thongbao4 =1; // ngay tra phai sau ngay cho vay
                        }
                    }
                    else
                    {
                        $this->view->thongbao3 =1;  // ngay cho vay va ngay tra phai tu nam 2000
                    }
                }
                else
                {
                    $this->view->thongbao2=1; // ngay chua dung
                }    
            }
            else
            {
                $this->view->thongbao1 = 1; // Thong bao chua dien du thong tin
            }
        }
    }
    
    //Thiet lap giao dich vay da tra
    public function setpaidAction()
    {   
        $u = $this->getUser();
        $this->view->user = $u;

        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        if($u->level != 1)
        {
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel("Modes");
        $v_model = $this->loadModel('Users');
        //$showall = $v_model->showall();
        //$this->view->showall=$showall;           
        if($this->getRequest()->isPost())
        {  
            $loan_id = $this->_getParam('edit');       
            $aa=$this->_getAllParams();          
            $status= $aa['statusz'];
            $a = $u_model->setpaid($status,$loan_id);    
        } 
        // Show tat ca ra
        //$showall = $v_model->showall();
        //$this->view->showall=$showall;
        $showall = $v_model->showall2($page_start,$page_rows);
        $this->view->showall=$showall;
        $num=$v_model->showall2_num();
        $this->view->num=$num;
    }
      
    // Danh sach user khong du dieu kien vay
    public function listunpaidAction()
    {
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->level !=1){
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }
        $u_model = $this->loadModel('Modes');
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        //$checkispaid = $u_model->checkispaid();
        //$this->view->ispaid = $checkispaid;
        
        $checkispaid = $u_model->checkispaid($page_start,$page_rows);
        $this->view->ispaid = $checkispaid;
        $num=$u_model->checkispaid_num();
        $this->view->num = $num;
    }
    
    public function warningoverdueAction()
    {
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->uid == 0)
        {
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }        
        $u_model = $this->loadModel('Modes');
        
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        $showall = $u_model->show_overdue($page_start,$page_rows);
        $this->view->showall=$showall;
        $num=$u_model->showoverdue_num();
        $this->view->num=$num;   
    }
    
    public function warningmaturityAction()
    {
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        $this->view->user = $u;
        if($u->uid == 0)
        {
            $this->_helper->getHelper('Redirector')->gotoUrlAndExit('user/login');
        }        
        $u_model = $this->loadModel('Modes');
        
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        $showall = $u_model->show_maturity($page_start,$page_rows);
        $this->view->showall=$showall;
        $num=$u_model->showmaturity_num();
        $this->view->num=$num;   
    }
    
    public function personalinfoAction()
    {
        $params=$this->_getAllParams(); 
        $page=$params['page'];
        $page_rows=5;
        $page_start=($page-1)*$page_rows;
        $this->view->test12=$params;
        
        $u = $this->getUser();
        if($u->uid == 0){
           $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
       }        
        $this->view->user = $u;
        $uid = $u->uid;
        $this->view->test=$uid;
        
        $u_model = $this->loadModel('Modes');   
        // Hien thi tham so
        $num1=$u_model->showoverdue_num();
        $this->view->num1=$num1[0]->num;
        
        $num2=$u_model->showmaturity_num();
        $this->view->num2=$num2[0]->num;
        
        if($this->getRequest()->isPost())
        {  
            $loan_id = $this->_getParam('edit');       
            $b=$this->_getAllParams();          
            $status= $b['status2'];
            $a = $u_model->setpaid($status,$loan_id);    
        }
  //    $username=$u_model->checkInfo($uid);
       // $info1=$u_model->checkinfo1($u->name,);
        //$this->view->info1=$info1;
  //     $this->view->info=$username;
        
        $info1 = $u_model->checkinfo1($u->name,$page_start,$page_rows);
        $this->view->info1=$info1;
        $num=$u_model->checkinfo1_num($u->name);
        $this->view->num=$num;
    }
}
?>
